Minor fix for _RasterizeToPixels back to avoid NaNs #235
+2
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a minor fix to avoid NaN values resulting from the backward pass of
_RasterizeToPixels
when padding is applied to the color tensor. Specifically, when the color tensor's feature dimension (COLOR_DIM in kernel) is not supported by a templated kernel, padding is applied as a preprocessing step with torch.empty to add channels up to the nearest supported COLOR_DIM. In this case, those empty values will be interpreted as undefined values during the backward pass which may populate the gradients formeans2d
,conics
, andopacities
with NaN values. Once the gradients for those three tensors have NaNs, the NaN values can propagate to other tensors and gradients. A simple fix is proposed in this pull request by explicitly initializing the padded channels' values with zero.At line 1081 in the corresponding backward kernel there's an iteration over the color dimensions for a summation which ultimately seems to be used to calculate the output gradient for
means2d
,conics
, andopacities
. At this point in the kernel, if any of the input color channels have a NaN, the summation's result is undefined. Thus, I don't see an obvious solution at the kernel-level without giving up register space, but initializing those padded channels with zeros explicitly solves the issue and based on my testing doesn't come with any performance drop.I tried creating a minimal working example of this issue for reproducibility, but since the empty tensor values' "initialization" is dependent on the program's memory space at execution, the example isn't guaranteed to be reproducible on a different machine. I'd be happy to update this request to include an explicit
dtype
in thetorch.zeros
initialization to matchcolor.dtype
if that is preferred for style. Thanks for considering this request.Thank you for this awesome project!